//
//  ChatTextView.m
//  输入框
//
//  Created by maple on 15/2/9.
//  Copyright (c) 2015年 maple. All rights reserved.
//

#import "ChatTextView.h"
#import "UIView+Frame.h"
#import "HPGrowingTextView.h"
#import "ContactKeyBoardCollectionView.h"
#import "EmojiView.h"
#import "KeyBoardManager.h"
#import "ChatRecordView.h"
#import <AVFoundation/AVFoundation.h>

#define kButtonWidth 30 //按钮宽度
#define kButtonHeight 30 //按钮高度
#define kButtonItemSpace 8 //按钮间距
#define kButtonY 9 //按钮的默认y

@interface ChatTextView () <HPGrowingTextViewDelegate,UICollectionViewDelegate,EmojiViewDelegate>

@property (nonatomic, strong) HPGrowingTextView *textView;
@property (nonatomic, strong) ContactKeyBoardCollectionView *keyboardCollectionView;
@property (nonatomic, strong) EmojiView *emojView;//表情的view
@property (nonatomic, strong) UIButton *pressBtn;//点压录音按钮
@property (nonatomic, strong) NSString *inputWords;//输入的文本
@property (nonatomic, strong) UIView *sepView;//下分割线

@property (nonatomic, assign) CGFloat baseDefaultY;
@property (nonatomic, assign) CGFloat addCollectionViewHeight;//其它view高度
@property (nonatomic, assign) CGFloat emojiViewHeight;//表情view高度

@property (nonatomic, strong) NSDate *recordStartTime;


@property (nonatomic, strong) ChatRecordView *revordView;//录音view



@end

@implementation ChatTextView

- (CGFloat)currentHeight
{
    return self.textView.height + 10;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.baseDefaultY = frame.origin.y;
        self.userInteractionEnabled = YES;
        // 设置背景
        self.backgroundColor = kBGColor;
        
        // 添加子控件
        [self setupSubViews];
        
        // 添加键盘弹出通知
        [self addKeyBoardNotification];
    }
    return self;
}
- (void)dealloc
{
    [self removeKeyBoardNotification];
}


#pragma mark -重写 HPGrowingTextView的方法
- (void)resignFirstResponder
{
    
    [self.textView resignFirstResponder];
    
}

- (void)becomeFirstResponder
{
    [self.textView becomeFirstResponder];
}

- (BOOL)isFirstResponder
{
    return [self.textView isFirstResponder];
}

#pragma mark - Public methods
/**
 *  隐藏textview
 */
- (void)hideSelfTextView
{
    if (self.emojButton.selected == YES || self.addBtn.selected == YES || [self.textView isFirstResponder]) {
        [self.textView resignFirstResponder];
        self.textView.text = nil;
        self.addBtn.selected = NO;
        self.emojButton.selected = NO;
        
        [UIView animateWithDuration:0.25 animations:^{
            self.y = self.superview.height - TextViewHeight;
        }];
    }
}
#pragma mark - EmojiViewDelegate

- (void)emojiView:(EmojiView *)emojiView selectStr:(NSString *)str
{
    self.textView.text = [self.textView.text stringByAppendingFormat:@"%@",str];
    self.inputWords = self.textView.text;
}
- (void)emojiViewDidSend:(EmojiView *)EmojiView
{
    self.inputWords = self.textView.text;

    if (self.delegate && [self.delegate respondsToSelector:@selector(chatTextView:sendMessage:)]) {
        [self.delegate chatTextView:self sendMessage:self.inputWords];
        self.textView.text = nil;
        self.inputWords = nil;
    }
}
- (void)emojiViewDidDelete:(EmojiView *)EmojiView
{
    if (self.textView.text.length > 0) {
        [self.textView.internalTextView deleteBackward];
        self.inputWords = self.textView.text;
    }
}
#pragma mark- UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 0 照片 1拍照 2小视频 3语音通话 4 视频聊天 5收藏
    if (self.delegate) {
        if (indexPath.row == 0 ||indexPath.row == 1) {
            if ([self.delegate respondsToSelector:@selector(chatTextView:openCameraOrAlbumTag:)]) {
                [self.delegate chatTextView:self openCameraOrAlbumTag:indexPath.row];
            }
        }
        if(indexPath.row == 2){
            if ([self.delegate respondsToSelector:@selector(chatTextSmallVideoView:)]) {
                [self.delegate chatTextSmallVideoView:self];
            }
        }
        if(indexPath.row == 3){
            if ([self.delegate respondsToSelector:@selector(chatTextSmallVideoView:)]) {
                [self.delegate chatTextVoiceView:self];
            }
        }
        if(indexPath.row == 4){
            if ([self.delegate respondsToSelector:@selector(chatTextSmallVideoView:)]) {
                [self.delegate chatTextViewRequestVideoChat:self];
            }
        }
        if(indexPath.row == 5){
            if ([self.delegate respondsToSelector:@selector(chatTextSmallVideoView:)]) {
                [self.delegate chatTextCollectListView:self];
            }
        }
    }
}

#pragma mark HPGrowingTextViewDelegate

// 文字即将换行
- (void)growingTextView:(HPGrowingTextView *)growingTextView willChangeHeight:(float)height
{
    float diff = (growingTextView.frame.size.height - height);
    self.height       -= diff;
    self.y            += diff;
    self.recordBtn.y  -= diff;
    self.addBtn.y     = self.recordBtn.y;
    self.emojButton.y = self.addBtn.y;
    self.sepView.y                = CGRectGetMaxY(self.recordBtn.frame) + kButtonItemSpace - 1;
    self.keyboardCollectionView.y = CGRectGetMaxY(self.recordBtn.frame) + kButtonItemSpace;
    self.emojView.y               = self.keyboardCollectionView.y;
}


- (BOOL)growingTextViewShouldBeginEditing:(HPGrowingTextView *)growingTextView
{
    // 还原内容
    self.textView.text = self.inputWords;
    [self addKeyBoardNotification];
    self.addBtn.selected = NO;
    self.emojButton.selected = NO;
    self.recordBtn.selected = NO;
    self.pressBtn.hidden = YES;
    
    if ([self.delegate respondsToSelector:@selector(chatTextViewShouldEditing:)]) {
        [self.delegate chatTextViewShouldEditing:self];
    }
    return YES;
}


- (BOOL)growingTextView:(HPGrowingTextView *)growingTextView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) { // 按了回车
    
        if (self.delegate && [self.delegate respondsToSelector:@selector(chatTextView:sendMessage:)]) {
            [self.delegate chatTextView:self sendMessage:self.inputWords];
            self.textView.text = nil;
            self.inputWords = nil;
        }
        return NO;
    }
    return YES;
}

- (void)growingTextViewDidChange:(HPGrowingTextView *)growingTextView
{
    if (self.textView.isFirstResponder) {
        self.inputWords = growingTextView.text;
    }
}

#pragma mark - Event response

/**
 touchDown 开始录音
 
 */
- (void)buttonTouchDown:(UIButton *)button
{
    if (![self canRecord]) {
        return;
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        if (self.delegate && [self.delegate respondsToSelector:@selector(chatTextViewBeginRecord:)]) {
            [self.delegate chatTextViewBeginRecord:self];
        }
        UIWindow *window = [[UIApplication sharedApplication] keyWindow];
        [window addSubview:self.revordView];
        [self.revordView updateRecordViewWithState:VoiceRecordState_Normal];
        self.recordStartTime = [NSDate date];
    });
   
}
-(BOOL)canRecord{
    // 先判读权限
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
    switch (status) {
        case AVAuthorizationStatusNotDetermined:
            //第一次
        {
            __block BOOL canRecord = YES;
            AVAudioSession *avSession = [AVAudioSession sharedInstance];
            if ([avSession respondsToSelector:@selector(requestRecordPermission:)]) {
                [avSession requestRecordPermission:^(BOOL granted) {
                
                    if (granted) {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            canRecord = YES;
                        });
                    }else{
                        dispatch_async(dispatch_get_main_queue(), ^{
                            UIAlertView *alte = [[UIAlertView alloc]initWithTitle:@"享受优质服务，需进行麦克风授权!" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去设置", nil];
                            alte.tag = 100;
                            [alte show];
                            canRecord = NO;
                        });
                        
                    }
                }];
            }
            return canRecord;
        }
            break;
            
        case AVAuthorizationStatusAuthorized:
            //授权
            return YES;
            break;
            
        default:
            //拒绝或限制的
        {
            UIAlertView *alte = [[UIAlertView alloc]initWithTitle:@"享受优质服务，需进行麦克风授权!" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去设置", nil];
            alte.tag = 100;
            [alte show];
            return NO;
        }
            break;
    }
    
    return YES;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        /**取消*/
    }
    if (buttonIndex == 1) {
        /**确定*/
        NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        [[UIApplication sharedApplication] openURL:url];
    }
}

/**
 touchUpOutside 控件范围外抬起  取消发送
 */
- (void)buttonTouchOutSide:(UIButton *)button
{
    
    [self.revordView removeFromSuperview];
    if (self.delegate && [self.delegate respondsToSelector:@selector(chatTextViewCancleEndRecord:)]) {
        [self.delegate chatTextViewCancleEndRecord:self];
    }
    
    
    
}
/**
 touchDragInside按下之后 ----控件之内-拖动--显示录制图片
 */
- (void)buttonTouchDragInSide:(UIButton *)button
{
//    [button setImage:[UIImage imageNamed:@"dd_record_release_end"] forState:UIControlStateNormal];
    [self.revordView updateRecordViewWithState:VoiceRecordState_Recording];
}

/**
 touchDragOutside按下之后 控件之外-拖动--显示取消图片
 */
- (void)buttonTouchDragOutSide:(UIButton *)button
{
    [self.revordView updateRecordViewWithState:VoiceRecordState_ReleaseToCancel];
//    [button setImage:[UIImage imageNamed:@"dd_record_release_end"] forState:UIControlStateNormal];
    
}


/**
 TouchDragEnter 当一次触摸从控件窗口之外拖动到内部时
 */
- (void)buttonTouchDragEnter:(UIButton *)button
{
    [self.revordView updateRecordViewWithState:VoiceRecordState_Recording];
//    [button setImage:[UIImage imageNamed:@"dd_record_release_end"] forState:UIControlStateNormal];
}
/**
 TouchDragExit 当一次触摸从控件窗口内部拖动到外部时
 */
- (void)buttonTouchDragExit:(UIButton *)button
{
   [self.revordView updateRecordViewWithState:VoiceRecordState_ReleaseToCancel];
//    [button setImage:[UIImage imageNamed:@"dd_record_release_end"] forState:UIControlStateNormal];
}

/**
 touchUpInside 正在录音---控件范围之内抬起---发送录音
 */
- (void)fileButtonAction:(UIButton *)sender {
    
    NSTimeInterval timeVal = [self compareCurrentTime:self.recordStartTime];
    if (timeVal <= 0.99) {
        [self.revordView updateRecordViewWithState:VoiceRecordState_RecordTooShort];
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(chatTextViewCancleEndRecord:)]) {
            [self.delegate chatTextViewCancleEndRecord:self];
        }
        
    }else{
        [self.revordView removeFromSuperview];
        self.revordView = nil;
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(chatTextViewEndRecord:)]) {
            [self.delegate chatTextViewEndRecord:self];
        }
        
    }
}
/**
 * 计算指定时间与当前的时间差
 * @param compareDate   某一指定时间
 * @return 多少(秒or分or天or月or年)+前 (比如，3天前、10分钟前)
 */
- (NSTimeInterval ) compareCurrentTime:(NSDate*) compareDate
//
{
    NSTimeInterval  timeInterval = [compareDate timeIntervalSinceNow];
    timeInterval = -timeInterval;
    //    long temp = 0;
    //    NSString *result;
    
    NSLog(@"timeInterval = %.1f",timeInterval);
    
    return timeInterval;
}




/**
 *  录音按钮按住
 */
- (void)beginRecord
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(chatTextViewBeginRecord:)]) {
        [self.delegate chatTextViewBeginRecord:self];
    }
}
/**
 *  录音按钮松开
 */
- (void)endRecord
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(chatTextViewEndRecord:)]) {
        [self.delegate chatTextViewEndRecord:self];
    }
}
- (void)cancleRecord
{
}


/**
 *  头部的录音按钮
 */
- (void)recordBtnClick
{
    self.addBtn.selected     = NO;
    self.emojButton.selected = NO;
    self.recordBtn.selected  = !self.recordBtn.selected;
    if (self.recordBtn.selected) {
        // 清空textView
        [self.textView resignFirstResponder];
        self.textView.text = nil;
        self.pressBtn.hidden = NO;
        self.addBtn.selected = NO;
        
        [UIView animateWithDuration:0.25 animations:^{
            self.y = self.superview.height - TextViewHeight;
        }];
    }else{
        [self.textView becomeFirstResponder];
        self.pressBtn.hidden = YES;
    }
}
/**
 *  显示出collectionView的view
 */
- (void)addBtnClick
{
    self.emojButton.selected           = NO;
    self.addBtn.selected               = !self.addBtn.selected;
    self.keyboardCollectionView.hidden = NO;
    self.emojView.hidden               = YES;
    [self updateKeyBoardWithHeight:self.addCollectionViewHeight];
    
    if (self.addBtn.selected) {
        [self textViewResignFirstResponder];
        
    }else{
        [self textViewBecomeFirstResponder];
    }
}
/**
 *  表情按钮关联的事件
 *
 *  @param button
 */
- (void)emojButtonAction:(UIButton *)button
{
    if (![self.textView isFirstResponder]) {
        [self.textView resignFirstResponder];
    }
    self.addBtn.selected               = NO;
    self.emojButton.selected           = !self.emojButton.selected;
    self.keyboardCollectionView.hidden = YES;
    self.emojView.hidden               = NO;
    [self updateKeyBoardWithHeight:self.emojiViewHeight];
    if (self.emojButton.selected) {
        [self textViewResignFirstResponder];
        
    }else{
        [self textViewBecomeFirstResponder];
    }
}

#pragma mark - SETTERS
- (ChatRecordView *)revordView
{
    if (_revordView == nil) {
        _revordView = [[ChatRecordView alloc]initWithFrame:CGRectMake(0, 0, 150, 150)];
        UIWindow *window = [[UIApplication sharedApplication] keyWindow];

        _revordView.center = window.center;
    }
    return _revordView;
}


#pragma mark - Private methods
// 添加子控件
- (void)setupSubViews
{
    // 录音按钮
    UIImage *image1 = [UIImage imageNamed:@"voice_button_image"];
    UIImage *image2 = [UIImage imageNamed:@"dd_input_normal"];
    self.recordBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.recordBtn.frame = CGRectMake(kButtonItemSpace, kButtonY, kButtonWidth, kButtonHeight);
    [self.recordBtn setBackgroundImage:image1 forState:UIControlStateNormal];
    [self.recordBtn setBackgroundImage:image2 forState:UIControlStateSelected];
    
    [self.recordBtn addTarget:self action:@selector(recordBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.recordBtn];
    
    // 加号按钮
    self.addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.addBtn.frame = CGRectMake(self.width - kButtonWidth - kButtonItemSpace, kButtonY, kButtonWidth, kButtonHeight);
    [self.addBtn setBackgroundImage:[UIImage imageNamed:@"more_button_image"] forState:UIControlStateNormal];
    [self.addBtn setBackgroundImage:[UIImage imageNamed:@"more_button_image"] forState:UIControlStateSelected];
    [self.addBtn addTarget:self action:@selector(addBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.addBtn];
#warning messaege -表情暂时隐藏-－修改对应输入框的大小
//    // 表情按钮 emojButtonBg
    self.emojButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.emojButton.frame = CGRectMake(CGRectGetMinX(self.addBtn.frame) - kButtonWidth - kButtonItemSpace, kButtonY, kButtonWidth, kButtonHeight);
    [self.emojButton setBackgroundImage:[UIImage imageNamed:@"expression_button_image"] forState:UIControlStateNormal];
    [self.emojButton setBackgroundImage:[UIImage imageNamed:@"expression_button_image"] forState:UIControlStateSelected];
    [self.emojButton addTarget:self action:@selector(emojButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.emojButton];
    

    
    // 输入框
    self.textView = [[HPGrowingTextView  alloc] init];
    self.textView.frame = CGRectMake(CGRectGetMaxX(self.recordBtn.frame) + kButtonItemSpace, 6, self.width - (kButtonWidth +  kButtonItemSpace)*4 - kButtonItemSpace+30, 36);
    self.textView.isScrollable = NO;
    self.textView.contentInset = UIEdgeInsetsMake(0, 2, 0, 0);
    self.textView.minNumberOfLines = 1;
    self.textView.maxNumberOfLines = 3;
    self.textView.returnKeyType = UIReturnKeySend;
    self.textView.font = [UIFont systemFontOfSize:15.0f];
    self.textView.delegate = self;
    self.textView.internalTextView.scrollIndicatorInsets = UIEdgeInsetsMake(5, 0, 5, 0);
    self.textView.backgroundColor = [UIColor whiteColor];
    self.textView.placeholder = @"请输入您想咨询的内容!";
    self.textView.layer.cornerRadius = 5.f;
    self.textView.layer.masksToBounds = YES;
    [self addSubview:self.textView];
    
    self.sepView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.recordBtn.frame) + kButtonItemSpace - 1, self.width, 1)];
    self.sepView.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:self.sepView];
    
    // 在输入框上添加按钮

    UIButton *pressBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    pressBtn.layer.borderWidth = 1.f;
    pressBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    pressBtn.layer.cornerRadius = 5.f;
    [pressBtn setBackgroundColor:self.backgroundColor];
    [pressBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [pressBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateHighlighted];
    pressBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [pressBtn setTitle:@"按住 说话" forState:UIControlStateNormal];
    [pressBtn setTitle:@"松开 结束" forState:UIControlStateHighlighted];

    //    [pressBtn addTarget:self action:@selector(beginRecord) forControlEvents:UIControlEventTouchDown];
//    [pressBtn addTarget:self action:@selector(endRecord) forControlEvents:UIControlEventTouchUpInside];
//    [pressBtn addTarget:self action:@selector(cancleRecord) forControlEvents:UIControlEventTouchUpOutside];
    
    [pressBtn addTarget:self action:@selector(buttonTouchDown:) forControlEvents:UIControlEventTouchDown];
    [pressBtn addTarget:self action:@selector(fileButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [pressBtn addTarget:self action:@selector(buttonTouchOutSide:) forControlEvents:UIControlEventTouchUpOutside];
    [pressBtn addTarget:self action:@selector(buttonTouchDragOutSide:) forControlEvents:UIControlEventTouchDragOutside];
    [pressBtn addTarget:self action:@selector(buttonTouchDragInSide:) forControlEvents:UIControlEventTouchDragInside];
    [pressBtn addTarget:self action:@selector(buttonTouchDragExit:) forControlEvents:UIControlEventTouchDragExit];
    [pressBtn addTarget:self action:@selector(buttonTouchDragEnter:) forControlEvents:UIControlEventTouchDragEnter];

    
    self.pressBtn = pressBtn;
    self.pressBtn.frame = self.textView.frame;
    [self addSubview:pressBtn];
    self.pressBtn.hidden = YES;
    
    [self installCollectionView];
    [self installEmojView];
}

- (void)installEmojView
{
    CGFloat keyboard = 216;
    if (IS_IPHONE6P) {
        keyboard = 271;
    }
    self.emojiViewHeight = [KeyBoardManager heightBaseKeyBoard];
    self.emojView = [[EmojiView alloc]initWithFrame:\
                     CGRectMake(0,CGRectGetMaxY(self.recordBtn.frame) + kButtonItemSpace, self.width, self.emojiViewHeight)
                                       isCommuntity:YES];
    self.emojView.delegate = self;
    [self addSubview:self.emojView];
}
- (void)installCollectionView
{
    CGFloat colectionViewCellWidth = (self.width - 50)/4;
    self.addCollectionViewHeight = [KeyBoardManager heightBaseKeyBoard];;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    [flowLayout setItemSize:CGSizeMake(colectionViewCellWidth,(self.addCollectionViewHeight - 10)/2)];
    flowLayout.minimumLineSpacing = 5;
    flowLayout.minimumInteritemSpacing = 7;
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    self.keyboardCollectionView = [[ContactKeyBoardCollectionView alloc]initWithFrame:\
                                   CGRectMake(kButtonItemSpace, CGRectGetMaxY(self.recordBtn.frame) + kButtonItemSpace, \
                                              self.width - kButtonItemSpace*2, self.addCollectionViewHeight) collectionViewLayout:flowLayout];
    self.keyboardCollectionView.delegate = self;
    [self addSubview:self.keyboardCollectionView];
    
}
#pragma mark - 点击按钮之后的底部view的相关操作
/**
 *  键盘消失
 */
- (void)textViewResignFirstResponder
{
    self.pressBtn.hidden = YES;
    self.recordBtn.selected = NO;
 
    [self.textView resignFirstResponder];
    // 移除键盘通知
    [self removeKeyBoardNotification];
}
/**
 *   键盘出现
 */
- (void)textViewBecomeFirstResponder
{
    [self addKeyBoardNotification];
    [self.textView becomeFirstResponder];
}
/**
 *  点击表情或add按钮之后的view偏移
 */
- (void)updateKeyBoardWithHeight:(CGFloat)height
{
    CGFloat keyboard = [KeyBoardManager heightBaseKeyBoard];
    if (self.y == (self.superview.height - self.currentHeight)) {
        [UIView animateWithDuration:0.25 animations:^{
            self.y -= keyboard;
        }];
    }
}

#pragma mark -－键盘的相关Notification
// 添加键盘通知
- (void)addKeyBoardNotification
{
//    [[NSNotificationCenter defaultCenter] addObserver:self
//                                             selector:@selector(keyBoardNotify:)
//                                                 name:UIKeyboardWillChangeFrameNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyBoardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyBoardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}

// 移除键盘通知
- (void)removeKeyBoardNotification
{
//    [[NSNotificationCenter defaultCenter]removeObserver:self
//                                                   name:UIKeyboardWillChangeFrameNotification
//                                                 object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self
                                                   name:UIKeyboardWillShowNotification
                                                 object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self
                                                   name:UIKeyboardWillHideNotification
                                                 object:nil];
}

- (void)keyBoardWillShow:(NSNotification *)aNotification
{
    NSDictionary *info = [aNotification userInfo];
    CGRect keyBFrm = [info[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    [UIView animateWithDuration:0.25 animations:^{
        // self.superview为输入框和拍照按钮的父view
        self.y = self.superview.height - keyBFrm.size.height - self.currentHeight;
    }];
}

- (void)keyBoardWillHide:(NSNotification *)aNotification
{
    if(self.addBtn.selected == YES || self.emojButton.selected == YES){
        CGFloat keyboard = [KeyBoardManager heightBaseKeyBoard];
        self.y = self.superview.height - self.currentHeight - keyboard;

//        if (self.y == (self.superview.height - self.currentHeight)) {
//            [UIView animateWithDuration:0.25 animations:^{
//            }];
//        }
    }else{
        [UIView animateWithDuration:0.25 animations:^{
            self.y = self.superview.height - TextViewHeight;
        }];
    }
    
}
// 键盘改变通知
- (void)keyBoardNotify:(NSNotification *)notify
{
//    NSDictionary *info = [notify userInfo];
//    CGRect keyBFrm = [info[UIKeyboardFrameEndUserInfoKey] CGRectValue];
//    
//    if(self.addBtn.selected == YES || self.emojButton.selected == YES){
//        CGFloat keyboard = [KeyBoardManager heightBaseKeyBoard];
//        if (self.y == (self.superview.height - self.currentHeight)) {
//            [UIView animateWithDuration:0.25 animations:^{
//                self.y -= keyboard;
//            }];
//        }
//    }else{
//        [UIView animateWithDuration:0.25 animations:^{
//            // self.superview为输入框和拍照按钮的父view
//            self.y = self.superview.height - keyBFrm.size.height - self.currentHeight;
//        }];
//    }
//    

}

@end
